import {
	createRouter,
	createWebHashHistory
	// createWebHistory
} from 'vue-router'
import store from '@/store/index.js'; // 假设你使用了 Vuex 来管理状态
import * as auth from '@/utils/auth.js';
import homePage from '@/views/home/homePage/index.vue'
import my from '@/views/me/my/index.vue'
import order from '@/views/order/index.vue'
import device from '@/views/devices/device/index.vue'
import scanCode from '@/views/register/index.vue'
import scanCode1 from '@/views/register/index1.vue'
import personDetals from '@/views/me/personDetals/index.vue'
import Login from '@/views/login.vue'
import Register from '@/views/register.vue'
import Test from '@/views/test.vue'
import Forget from '@/views/ForgetPassword.vue'
import Settled from '@/views/home/Settled/index.vue'
import Record from '@/views/home/record/index.vue'
import ConsumptionVip from '@/views/home/consumptionVip/index.vue'
import Blacklist from '@/views/home/blacklist/index.vue'
import DataRecharge from '@/views/home/DataRecharge/index.vue'
import BootConfiguration from '@/views/home/BootConfiguration/index.vue'
import BootConfigurationDetails from '@/views/home/BootConfigurationDetails/index.vue'
import DeliveryAddress from '@/views/home/DeliveryAddress/index.vue'
import transferRecord from '@/views/home/transferRecord/index.vue'

const router = createRouter({
	history: createWebHashHistory(import.meta.env.BASE_URL),
	routes: [{
			path: '/login',
			name: 'login',
			component: Login,
			meta: {
				hideTabBar: true
			}
		},
		{
			path: '/test',
			name: 'test',
			component: Test,
			meta: {
				hideTabBar: true
			}
		},
		{
			path: '/register',
			name: 'register',
			component: Register,
			meta: {
				hideTabBar: true
			}
		},
		{
			path: '/forget',
			name: 'forget',
			component: Forget,
			meta: {
				hideTabBar: true
			}
		},
		{
			path: '/',
			redirect: '/login',
			meta: {
				hideTabBar: true
			}
		},
		{
			path: '/homePage',
			name: 'homePage',
			component: homePage,
			meta: {
				requiresAuth: true
			} // 表示这个路由需要认证
		},
		{
			path: '/my',
			name: 'my',
			component: my,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/order',
			name: 'order',
			component: order,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/device',
			name: 'device',
			component: device,
			meta: {
				requiresAuth: true
			}
		},
		{
		  path: '/scanCode',
		  name: 'scanCode',
		  component: scanCode,
		},
		{
		  path: '/scanCode1',
		  name: 'scanCode1',
		  component: scanCode1,
		},
		{
			path: '/personDetals',
			name: 'personDetals',
			component: personDetals,
			meta: {
				requiresAuth: true,
				hideTabBar: true
			}
		},
		{
			path: '/settled',
			name: 'settled',
			component: Settled,
			meta: {
				requiresAuth: true,
				hideTabBar: true
			}
		},
		{
			path: '/record',
			name: 'record',
			component: Record,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/consumptionVip',
			name: 'consumptionVip',
			component: ConsumptionVip,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/blacklist',
			name: 'blacklist',
			component: Blacklist,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/dataRecharge',
			name: 'dataRecharge',
			component: DataRecharge,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/bootConfiguration',
			name: 'bootConfiguration',
			component: BootConfiguration,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/bootConfigurationDetails/:deviceId/:data',
			name: 'bootConfigurationDetails',
			component: BootConfigurationDetails,
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/deliveryAddress',
			name: 'deliveryAddress',
			component: DeliveryAddress,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/deliveryAddressEdit/:id/:data',
			name: 'deliveryAddressEdit',
			component: () => import('@/views/home/DeliveryAddressEdit/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/transferRecord',
			name: 'transferRecord',
			component: transferRecord,
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/secondaryAccount',
			name: 'secondaryAccount',
			component: () => import('@/views/me/secondaryAccount/index.vue'),
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/secondaryAccountAdd',
			name: 'secondaryAccountAdd',
			component: () => import('@/views/me/secondaryAccountAdd/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/secondaryAccountUpdate/:id/:storeIds',
			name: 'secondaryAccountUpdate',
			component: () => import('@/views/me/secondaryAccountUpdate/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/incomeInfo',
			name: 'incomeInfo',
			component: () => import('@/views/home/incomeInfo/index.vue'),
			meta: {
				requiresAuth: true
			}
		},
		{
			path: '/updatePassword',
			name: 'updatePassword',
			component: () => import('@/views/me/updatePassword/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/binding/:deviceNumber',
			name: 'binding',
			component: () => import('@/views/home/binding/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/codeImage/:qmCode',
			name: 'codeImage',
			component: () => import('@/views/devices/codeImage/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/payCode/:platform?/:gdyLink?', // 加问号表示可选
			// path: '/payCode/:platform/:gdyLink',
			name: 'payCode',
			component: () => import('@/views/home/payCode/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/incomeInfoDetails/:values',
			name: 'incomeInfoDetails',
			component: () => import('@/views/home/incomeInfoDetails/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/incomeInfoDevice/:values',
			name: 'incomeInfoDevice',
			component: () => import('@/views/home/incomeInfoDevice/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		{
			path: '/BootupdatePulse/:data',
			name: 'BootupdatePulse',
			component: () => import('@/views/home/BootupdatePulse/index.vue'),
			meta: {
				hideTabBar: true,
				requiresAuth: true
			}
		},
		// 404 页面
		{
			path: '/:pathMatch(.*)*',
			name: 'NotFound',
			component: () => import('@/views/NotFound/index.vue'),
			meta: {
				hideTabBar: true
			}
		},
	],
});


router.beforeEach((to, from, next) => {
	const isAuthenticated = auth.getToken()

	// if (to.matched.length === 0) { // 没有匹配到任何路由
	// 	next({
	// 		name: 'NotFound'
	// 	})
	// } else {
	// 	next()
	// }


	// 需要认证且未登录的情况
	if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated) {
		next({
			path: '/login'
		})
		return
	}

	// 已登录时禁止访问登录/注册页
	if ((to.path === '/login' || to.path === '/register' || to.path === '/forget') && isAuthenticated) {
		next({
			path: '/homePage'
		})
		return
	}

	next()
})

export default router
